using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_ArcherElfArrow : MonoBehaviour, ICounterable
{
    private Rigidbody2D rb;
    private Collider2D col;
    private Animator animator;
    private Entity_Combat combat;
    [SerializeField] private LayerMask whatIsTarget;

    public bool CanStunned => true;



    public void SetupArrow(float xVelocity, Entity_Combat combat)
    {
        rb = GetComponent<Rigidbody2D>();
        col = GetComponent<Collider2D>();
        animator = GetComponentInChildren<Animator>();

        this.combat = combat;
        
        rb.velocity = new(xVelocity, 0);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (((1 << collision.gameObject.layer) & whatIsTarget) != 0)
        {
            combat.PerformAttack(collision.transform);
            StuckIntoTarget(collision.transform);
        }
    }
    private void StuckIntoTarget(Transform target)
    {
        rb.velocity = Vector2.zero;
        rb.bodyType = RigidbodyType2D.Kinematic;
        col.enabled = false;
        animator.enabled = false;
        
        transform.parent = target;

        Destroy(gameObject, 1);
    }

    public void HanderCounter()
    {
        rb.velocity = new(-rb.velocity.x, 0);
        transform.Rotate(0, 180, 0);

        int enemyLayer = LayerMask.NameToLayer("Enemy");

        //将enemyLayer层添加到whatIsTarget中
        whatIsTarget |= 1 << enemyLayer;
    }
}
